<template>
  <ant-modal
    modalWidth="1300"
    modalHeight="600"
    :visible="open"
    modal-title="选择销售单"
    :adjust-size="true"
    @cancel="cancel"
  >
    <a-row slot="content">
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6" >
              <a-form-item label="查询条件">
                <a-input v-model="queryParam.orderParams" placeholder="请输入查询条件" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            
            <a-col :span="6">
              <a-form-item label="单据阶段">
                <a-select placeholder="请选择状态" v-model="statusArr" style="width: 100%" allow-clear mode="multiple">
                  <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{
                    d.dictLabel
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-divider />
    <advance-table
      :components="drag(columns,'bizOrderSelectList')"
      columnsTitle="bizOrderSelectList"
      :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
      tableKey="base-bizProduct-index-table"
      @change="handleTableChange"
      rowKey="id"
      size="middle"
      @refresh="getList"
      :columns="columns"
      :data-source="list"
      :loading="loading"
      :format-conditions="true"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type:'radio' }"
    >
    
    <span slot="projectSlot" slot-scope="{ record }">
          <span v-if="record.projectNumber">
            {{ record.projectNumber }}
          </span>
          <span v-else> -- </span>
        </span>
        <span slot="customerId" slot-scope="{ record }">
          {{ customerIdFormat(record) }}
        </span>
        <span slot="isSettleSend" slot-scope="{ record }">
          {{ isSettleSendFormat(record) }}
        </span>
        <span slot="orderType" slot-scope="{ record }">
          {{ orderTypeFormat(record) }}
        </span>
        <span slot="auditRender" slot-scope="{ record }">
          <span v-if="record.auditStatus == 1">待审核</span>
            <span v-else>已审核</span>
        </span>

        <span slot="sinkSlot" slot-scope="{ record }">
          <span v-if="record.sinkArrival" ><a>水槽已到</a></span>
          <span v-else  style="color: #0099cc"><a>水槽未到</a></span>
        </span>
        <span slot="statusRender" slot-scope="{ record }">
          {{ statusFormat(record) }}
        </span>
        <span slot="installType" slot-scope="{ record }">
          {{ installFormat(record) }}
        </span>
        <span slot="deliveryType" slot-scope="{ record }">
          {{ deliveryFormat(record) }}
        </span>
    </advance-table>
    </a-row>
    <template slot="footer">
      <a-button :disabled="disabled" @click="cancel">
        取消
      </a-button>
      <a-button type="primary" :disabled="disabled" @click="confirm(true)">
        确定
      </a-button>
    </template>
    
  </ant-modal>
</template>
<script>

import AdvanceTable from '@/components/pt/table/AdvanceTable'
import AntModal from '@/components/pt/dialog/AntModal'
import { listBizOrder ,getInitData} from '@/api/biz/bizOrder'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
export default {
  name: 'BizProduct',
  components: {
    AdvanceTable,
    AntModal
  },
  mixins:[tableDragResizeMixin],
  data () {
    return {
      disabledCreated:true,
      productId:undefined,
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      list:[],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        processName: undefined,
        inventoryFlag:'',
        processCode: undefined

      },
      
      columns: [
        {
          title: '项目编号',
          dataIndex: 'projectNumber',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'projectSlot' },
        },
        {
          title: '订单编号',
          dataIndex: 'orderNumber',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '客户',
          dataIndex: 'customerName',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '部门',
          dataIndex: 'deptIdName',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '下单日期',
          dataIndex: 'orderDate',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '送货地址',
          dataIndex: 'address',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '业主电话',
          dataIndex: 'phone',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '设计师',
          dataIndex: 'billerName',
          align: 'center',
        },
        {
          title: '开单员',
          dataIndex: 'createByName',
          align: 'center',
        },
        {
          title: '开单日期',
          dataIndex: 'receiveDate',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '安装日期',
          dataIndex: 'installDate',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '工序进度',
          scopedSlots: { customRender: 'record' },
          align: 'center',
        },
        {
          title: '清账发货',
          dataIndex: 'isSettleSend',
          scopedSlots: { customRender: 'isSettleSend' },
          align: 'center',
        },
        {
          title: '成品位置',
          dataIndex: 'productWarehouse',
          align: 'center',
        },
        {
          title: '安装模式',
          dataIndex: 'installType',
          align: 'center',
          scopedSlots: { customRender: 'installType' },
        },
        {
          title: '配送模式',
          dataIndex: 'deliveryType',
          align: 'center',
          scopedSlots: { customRender: 'deliveryType' },
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          scopedSlots: { customRender: 'orderType' },
          align: 'center',
        },
        {
          title: '销售金额',
          dataIndex: 'saleAmount',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '收款金额',
          dataIndex: 'collectedAmount',
          ellipsis: true,
          align: 'center',
        },
        {
          title: '审核状态',
          dataIndex: 'auditStatus',
          align: 'center',
          scopedSlots: { customRender: 'auditRender' },
        },
        {
          title: '单据阶段',
          dataIndex: 'orderStatus',
          align: 'center',
          scopedSlots: { customRender: 'statusRender' },
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' },
        },
      ],
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      disabled: false,
      id: undefined,
      isSettleSendOptions:[],
      orderTypeOptions:[],
      statusOptions:[],
      deliveryOptions:[],
      installOptions:[],
      statusArr:[],
    }
  },
  created () {
    this.initColumnsSetting('bizOrderSelectList')
    getInitData('order_type,sys_yes_no,order_status,install_type,delivery_type').then((response) => {
      this.isSettleSendOptions = response.data.sys_yes_no
      this.orderTypeOptions = response.data.order_type
      this.statusOptions = (response.data.order_status || []).filter(v=>!(['8'].includes(v.dictValue)) )
      this.deliveryOptions = response.data.delivery_type
      this.installOptions = response.data.install_type
    })
  },
  methods: {
    /** 查询生产工序列表 */
    getList () {
      if(this.statusArr && this.statusArr.length){
        this.queryParam.orderStatus = this.statusArr.join(',')
      }
      listBizOrder(this.queryParam).then(res => {
        if(res.code == 200) {
          this.list = res.data.list
          this.total = res.data.total
          this.loading = false
        }else{
          this.message.error(res.msg)
        }
      })
    },
    
    // 清账发货字典翻译
    isSettleSendFormat(row) {
      if (row.isSettleSend) {
        return this.selectDictLabel(this.isSettleSendOptions, row.isSettleSend)
      } else {
        return ''
      }
    },
    // 订单类型字典翻译
    orderTypeFormat(row) {
      if (row.orderType) {
        return this.selectDictLabel(this.orderTypeOptions, row.orderType)
      } else {
        return ''
      }
    },
    // 客户字典翻译
    customerIdFormat(row) {
      if (row.customerId) {
        return this.selectDictLabel(this.customerIdOptions, row.customerId)
      } else {
        return ''
      }
    },
    statusFormat(row) {
      return this.selectDictLabel(this.statusOptions, row.orderStatus)
    },
    // 配送模式
    deliveryFormat(row) {
      return this.selectDictLabel(this.deliveryOptions, row.deliveryType)
    },
    // 安装模式
    installFormat(row) {
      return this.selectDictLabel(this.installOptions, row.installType)
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    handleTableChange (pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },

    // 取消按钮
    cancel () {
      this.open = false
      this.$emit('close')
    },
    /** 提交按钮 */
    confirm: function (closeDialog) {
     // this.closeDialog = closeDialog
      this.disabled = true
      if(this.selectedRowKeys.length==0) {
        this.$message.warning('请选择销售订单');
        this.disabled = false
        return false
      } else {
        //保存产品
        this.open = false
        this.$emit('ok',this.selectedRows);
        this.$emit('close')
        this.disabled = false
      }
    },
    show() {
      this.open = true;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,

        productCode: undefined,

        mnemonicCode: undefined

      }
      this.handleQuery()
    }
  }
}
</script>
